import { routerRedux } from 'dva/router';
import api from '../api';
import { message } from 'antd'

export default {
    namespace: 'combinatorialAnalysis',
    state: {




        lPortId: null,// 产品组合ID 默认值 产品ID排序第一的产品 || query中的 lPortId
        cBenchCode: null,// 业绩基准ID 默认值 沪深300 || query中的 cBenchCode
        ratioBegindate: null, // 分类占比 开始时间 null
        ratioEnddate: null, // 分类占比 结束时间 null
        industryID: '38_1', // 分类占比 行业id	null
        trendBegindate: null, // 仓位走势 开始时间 null
        trendEnddate: null, // 仓位走势 结束时间  null
        timeRange: '2', // 净值走势时间段 默认值 最近一月



        // realTimeNetValue
        latestNetValue: null, // 最新净值 默认值 null
        realTimeNetValue: {}, // 实时净值 默认值 	null
        unitNetValueTrend: [], // 单位净值走势 默认值 []
        totalNetValueTrend: [], // 累计净值走势 默认值 []
        netValuePerformance: [],// 净值表现 默认值 []
        monthlyReturn: [],// 月度回报数据 默认值 []

        portfolioAssets: {},// 组合资产 默认值 {} 
        categoryAssets: {},// 类别资产 默认值 {} 
        industryDistribution: {},//行业分布 默认值 {} 
        trend: [],//仓位走势 默认值{} 
        topTenStocks: {}, //前十大重仓骨片 默认值{} 

        stockTopGood: {},
        stockTopBad: {},
        brinson: {}

    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                // 没有参数时，向后端请求数据，并且改变query
                if (pathname.indexOf('/customerService/combinatorialAnalysis') > -1) {
                    dispatch({ type: 'init', payload: query })
                }
            })
        }
    },
    effects: {
        * init({
            payload
        }, { call, put, select }) {
            // 初始化
            const state = yield select(state => state.combinatorialAnalysis)
            // 看本次的参数和上次的参数是否一致：
            // 一致的话，不请求，不一致的话，正常请求
            // 定义一个对象用于定义参数
            const newObj = {}
            for (var key in payload) {
                newObj[key] = state[key]
            }
            if (!Object.getOwnPropertyNames(payload).length || JSON.stringify(payload) != JSON.stringify(newObj)) {
                payload.date = payload.date || null
                yield put({
                    type: 'setState',
                    payload,
                })
                if (payload.lPortId && !state.beginDate) {
                    yield put({
                        type: 'getPositionInit', payload
                    })
                }
            }
        },
        * getPositionInit({
            payload
        }, { call, put, select }) {

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)


            //获取初始化数据
            const res = yield call(api.position.getPositionInit, query)
            const init = res.data
            init.ratioBegindate = init.beginDate
            init.ratioEnddate = init.endDate
            init.trendBegindate = init.beginDate
            init.trendEnddate = init.endDate
            yield put({
                type: 'setState', payload: init
            })
        },

        * getLatestNetValue({
            payload
        }, { call, put, select }) {
            // 获取最新净值数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)

            const res = yield call(api.netValue.getLatestNetValue, query)
            yield put({
                type: 'setState', 
                payload: {
                    latestNetValue:res.data,
                }
            })
        },
        * getRealTimeNetValue({
            payload
        }, { call, put, select }) {
            // 获取实时净值数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)

            const res = yield call(api.netValue.getRealTimeNetValue, query)
            yield put({
                type: 'setState', payload: {
                    realTimeNetValue:res.data,
                }
            })
        },

        * getNetValueTrend({
            payload
        }, { call, put, select }) {
            // 获取净值走势数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.timeRange && (query.timeRange = payload.timeRange)


            const res = yield call(api.netValue.getNetValueTrend, query)
            yield put({
                type: 'setState', payload: {
                    netValueTrend:res.data,
                }
            })
        },

        * getNetValuePerformance({
            payload
        }, { call, put, select }) {
            // 获取净值表现数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)

            const res = yield call(api.netValue.getNetValuePerformance, query)
            yield put({
                type: 'setState', payload: {
                    netValuePerformance:res.data,
                }
            })
        },

        * getMonthlyReturn({
            payload
        }, { call, put, select }) {
            // 获取月度回报数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)

            const res = yield call(api.netValue.getMonthlyReturn, query)
            yield put({
                type: 'setState', payload: {
                    monthlyReturn:res.data,
                }
            })
        },


        * getPositionInit({
            payload
        }, { call, put, select }) {

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)


            //获取持仓分析初始化数据
            const res = yield call(api.position.getPositionInit, query)
            const init = res.data
            init.ratioBegindate = init.beginDate
            init.ratioEnddate = init.endDate
            init.trendBegindate = init.beginDate
            init.trendEnddate = init.endDate
            yield put({
                type: 'setState', payload: init
            })
        },


        * getPortfolioAssets({
            payload
        }, { call, put, select }) {

            let query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate || payload.lastDate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate || payload.lastDate)
            payload.productId && (query.lPortId = payload.productId)
            // payload.date && (query.begindate = payload.date)
            // payload.date && (query.enddate = payload.date)


            //获取组合资产数据  
            const res = yield call(api.position.getPortfolioAssets, query)
            yield put({
                type: 'setState', payload: {
                    portfolioAssets:res.data,
                }
            })
        },

        * getPortfolioAssets2({
        payload
    }, { call, put, select }) {

        let query = {}
        payload.lPortId && (query.lPortId = payload.lPortId)
        payload.productId && (query.lPortId = payload.productId)
        payload.lastDate && (query.begindate = payload.lastDate)
        payload.lastDate && (query.enddate = payload.lastDate)
        payload.date && (query.begindate = payload.date)
        payload.date && (query.enddate = payload.date)


        //获取组合资产数据  
        const res = yield call(api.position.getPortfolioAssets, query)
        yield put({
            type: 'setState', payload: {
                portfolioAssets:res.data,
            }
        })
    },

        * getCategoryAssets({
            payload
        }, { call, put, select }) {
            //获取类别资产数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate || payload.lastDate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate || payload.lastDate)
            payload.productId && (query.lPortId = payload.productId)
            // payload.date && (query.begindate = payload.date)
            // payload.date && (query.enddate = payload.date)

            const res = yield call(api.position.getCategoryAssets, query)
            yield put({
                type: 'setState', payload: {
                    categoryAssets:res.data
                }
            })
        },

        * getCategoryAssets2({
        payload
    }, { call, put, select }) {
        //获取类别资产数据
        const query = {}
        payload.lPortId && (query.lPortId = payload.lPortId)
        payload.productId && (query.lPortId = payload.productId)
        payload.lastDate && (query.begindate = payload.lastDate)
        payload.lastDate && (query.enddate = payload.lastDate)
        payload.date && (query.begindate = payload.date)
        payload.date && (query.enddate = payload.date)

        const res = yield call(api.position.getCategoryAssets, query)
        yield put({
            type: 'setState', payload: {
                categoryAssets:res.data
            }
        })
    },

        * getIndustryDistribution({
            payload
        }, { call, put, select }) {
            //获取行业分布数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate)
            payload.industryID && (query.indunum = payload.industryID)

            const res = yield call(api.position.getIndustryDistribution, query)
            yield put({
                type: 'setState', payload: {
                    industryDistribution:res.data
                }
            })
        },

        * getPositionTrend({
            payload
        }, { call, put, select }) {
            //获取仓位走势数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.trendBegindate && (query.begindate = payload.trendBegindate)
            payload.trendEnddate && (query.enddate = payload.trendEnddate)

            const res = yield call(api.position.getPositionTrend, query)
            yield put({
                type: 'setState', payload: {
                    trend:res.data
                }
            })
        },

        * getTopTenStocks({
            payload
        }, { call, put, select }) {
            //获取前十大重仓股票数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.trendBegindate && (query.begindate = payload.trendBegindate)
            payload.trendEnddate && (query.enddate = payload.trendEnddate)


            const res = yield call(api.position.getTopTenStocks, query)
            yield put({
                type: 'setState', payload: {
                    topTenStocks:res.data
                }
            })
        },

        * getStockTopGood({
            payload
        }, { call, put, select }) {

            // 获取前5大主要贡献数据

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.timeRange && (query.timeRange = payload.timeRange)
            query.type = "good"
            query.num = 5

            const res = yield call(api.performanceAttribution.getStockTop, query)
            const stockTopGood = res.data
            stockTopGood.list&&stockTopGood.list.map((item, i) => {
                item.id = i + 1
            })
            yield put({
                type: 'setState', payload: {
                    stockTopGood,
                }
            })
        },

        * getStockTopBad({
            payload
        }, { call, put, select }) {

            // 获取前5大主要拖累数据

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.timeRange && (query.timeRange = payload.timeRange)
            query.type = "bad"
            query.num = 5

            const res = yield call(api.performanceAttribution.getStockTop, query)
            const stockTopBad = res.data
            stockTopBad.list&&stockTopBad.list.map((item, i) => {
                item.id = i + 1
            })
            yield put({
                type: 'setState', payload: {
                    stockTopBad,
                }
            })
        },
        * getBrinson({
            payload
        }, { call, put, select }) {

            // 获取股票归因数据

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.timeRange && (query.timeRange = payload.timeRange)


            const res = yield call(api.performanceAttribution.getBrinson, query)
            yield put({
                type: 'setState', payload: {
                    brinson:res.data,
                }
            })
        },

        * getStockHolding({
            payload
        }, { call, put, select }) {
            //获取股票持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getStockHolding, query)
            yield put({
                type: 'setState', payload: {
                    stockHolding:res.data
                }
            })
        },


        * getBondHolding({
            payload
        }, { call, put, select }) {
            //获取债券持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getBondHolding, query)
            yield put({
                type: 'setState', payload: {
                    bondHolding:res.data
                }
            })
        },
        * getFundHolding({
            payload
        }, { call, put, select }) {
            //获取基金持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getFundHolding, query)
            yield put({
                type: 'setState', payload: {
                    fundHolding:res.data
                }
            })
        },
        * getCashHolding({
            payload
        }, { call, put, select }) {
            //获取现金持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getCashHolding, query)
            yield put({
                type: 'setState', payload: {
                    cashHolding:res.data
                }
            })
        },

        * getDerivativesHolding({
            payload
        }, { call, put, select }) {
            //获取衍生品持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getDerivativesHolding, query)
            yield put({
                type: 'setState', payload: {
                    derivativesHolding:res.data
                }
            })
        },
        * getOtherHolding({
            payload
        }, { call, put, select }) {

            //获取其他持仓数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.through.getOtherHolding, query)
            yield put({
                type: 'setState', payload: {
                    otherHolding:res.data
                }
            })
        },


        * getRealisationSummary({
            payload
        }, { call, put, select }) {
            //获取变现数据汇总数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.lastDate && (query.date = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.risk.getRealisationSummary, query)
            yield put({
                type: 'setState', payload: {
                    realisationSummary: res.data
                }
            })
        },

        * getRealisationList({
            payload
        }, { call, put, select }) {
            //获取变现数据列表数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)
            // payload.lastDate && (query.begindate = payload.lastDate)
            // payload.lastDate && (query.enddate = payload.lastDate)
            payload.lastDate && (query.date = payload.lastDate)
            payload.date && (query.date = payload.date)

            const res = yield call(api.risk.getRealisationList, query)
            yield put({
                type: 'setState', payload: {
                    realisationList: res.data
                }
            })
        },

        * getStyleResearchList({
            payload
        }, { call, put, select }) {
            //获取风格研究列表数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)

            const res = yield call(api.style.getStyleResearchList, query)
            yield put({
                type: 'setState', payload: {
                    styleResearchList: res.data
                }
            })
        },

        * getManagerStyleBoxList({
            payload
        }, { call, put, select }) {
            //获取管理人风格列表数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)

            const res = yield call(api.style.getManagerStyleBoxList, query)
            yield put({
                type: 'setState', payload: {
                    managerStyleBoxList: res.data
                }
            })
        },

        * getManagerStyleBoxCharts({
            payload
        }, { call, put, select }) {
            //获取管理人风格图表数据

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.productId && (query.lPortId = payload.productId)

            const res = yield call(api.style.getManagerStyleBoxCharts, query)
            yield put({
                type: 'setState', payload: {
                    managerStyleBoxCharts: res.data
                }
            })
        },

        * getReturnValuation({
            payload
        }, { call, put, select }) {
            //获取各风格回报和估值数据
            const query = {}
            // payload.lPortId && (query.lPortId = payload.lPortId)
            // payload.productId && (query.lPortId = payload.productId)

            const res = yield call(api.style.getReturnValuation, query)
            yield put({
                type: 'setState', payload: {
                    returnValuation: res.data
                }
            })
        },



    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload
            }
        },
        initState(state) {
            return {}
        }
    }
}
